<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

class Application {
    private $mSession;
    private $mForbiddenAccess = false; //flag untuk menentukan apakah ditemukan Forbidden Access. Jika bernilai true maka terdapat proses yg tidak dapat dilanjutkan
    private $mCurrentUrl;
    private $mTheme;
    private $mLogged;
    
    private $mError;
    private $mErrorHandler;
    private $mErrorMessage = array();
    private $mOnloadJS;
    private $mSessionHandlerConig;

    public function __construct($mSession=null) {
        $this->mSession = $mSession;

        //GET CURRENT URL
        $tCurrentUrl = $_SERVER['REQUEST_URI'];
        $tPos = strpos($tCurrentUrl, 'main.php');
        $this->mCurrentUrl = substr($tCurrentUrl, $tPos);

        //SET THEME
        $this->mTheme = new ThemeBasic();
        if($mSession!=null){
            $this->mTheme->SetInfoUserName($this->mSession->mVars[User][Name]);
            $this->mTheme->SetInfoUserLevel($this->mSession->mVars[User][Auth]);
        }
    }

    /* ===== *//* ===== *//* ===== *//* ===== *//* ===== *//* ===== *//* ===== */
    /*                        O U T P U T                                      */



    public function PrintPage() {
        if(!empty($this->mOnloadJS) )
            echo "\n<body onLoad=\"$this->mOnloadJS\">";
        else
            echo "\n<body>";
        
        $this->mTheme->PrintMainPage($this->mError);
        echo "</body>\n";
        
        if ($this->mError) {
            $this->PrintError($this->mError[ERROR_SECTION], $this->mError['ERROR_CODE']);
            echo '<script>$("html, body").animate({ scrollTop: 0 }, "slow");</script>';
        }
    }
    
    public function PrintPageWithoutTheme() {
        include_once($this->mTheme->GetPage($this->mError) );
    }

    public function PrintGraphicPage() {
        $this->mTheme->PrintMainGraphicPage($this->mError);

        if ($this->mError) {
            $this->PrintError($this->mError[ERROR_SECTION], $this->mError['ERROR_CODE']);
            echo '<script>$("html, body").animate({ scrollTop: 0 }, "slow");</script>';
                      //  echo '<script>$("#dialog-ErrorMessage").dialog({autoOpen:true});</script>';

        }
    }

    public function GotToRestrictedPage() {
        //GO TO RESTRICTED PAGE adalah me-redirect halaman ke halaman forbidden access/kosong (tidak dpt memanipulasi/ tdk ada output)
        //dalam hal ini melakukan perubahan SetPage tidak memungkinkan dikarenakan Inisialisasi Level Akses lebih dulu ketimbang SetPage.
        //Solusi: dengan menggunankan flag Forbidden Access dimana jika bernilai true maka SetPage menjadi DISABLE
        $this->SetPage("forbidden.php");
        $this->mForbiddenAccess = true;
    }

    public function GetRefererUrl() {
        $tRef = $_SERVER["HTTP_REFERER"];
        $tTempRef = explode("?", $_SERVER["HTTP_REFERER"]);
        $tRef = $tTempRef[1];
        return $tRef;
    }

    /* ===== *//* ===== *//* ===== *//* ===== *//* ===== *//* ===== *//* ===== */
    /*                              M U T U A T O R                            */
    /* ===== *//* ===== *//* ===== *//* ===== *//* ===== *//* ===== *//* ===== */
    
    public function SetOnloadJS($pJSFunctionName){
        $this->mOnloadJS= $pJSFunctionName;
    }
    
    public function GetCurrentUrl() {
        return $this->mCurrentUrl;
    }

    public function SetPage($pUrl) {
        if (!$this->mForbiddenAccess)
            $this->mTheme->SetPage($pUrl);
    }
    
    public function SetLevelAccess($pLevel) {
        $this->mLogged = new Logged($this->mSession);
        $tResult = $this->mLogged->SetLevelAccess($pLevel);

        if (!$tResult) {
           // $this->GotToRestrictedPage();
        }
        //$this->SetPage("restricted");
        //die();
    }
    
    public function SetNotification($pNewsArray){
        /*
         * NEWS ARRAY
         * ==========
         * Array Index: Id Container (div)
         */
        
        if(is_array($pNewsArray) )
            $this->mTheme->SetNotification($pNewsArray);
    }
    
    public function SetMenu($pMenu) {
        $this->mTheme->SetMenu($pMenu);
    }

    /* ===== *//* ===== *//* ===== *//* ===== *//* ===== *//* ===== *//* ===== */


    /* ===== *//* ===== *//* ===== *//* ===== *//* ===== *//* ===== *//* ===== */
    /*                       E R R O R   H A N D L E R                         */
    /* ===== *//* ===== *//* ===== *//* ===== *//* ===== *//* ===== *//* ===== */

    public function SetErrorMessage($_ERRORS) {
        $this->mErrorMessage = $_ERRORS;
    }

    public function PrintError($pErrorSection, $pErrorCode) {
        $this->mErrorHandler = new ErrorHandler($this->mErrorMessage);
        $this->mErrorHandler->SetErrorSection($pErrorSection);
        $this->mErrorHandler->SetErrorCode($pErrorCode);
        
        echo "\n<div id=\"dialog-ErrorMessage\" >\n".
             "<div class=\"ErrorMessage-Container\">".$this->mErrorHandler->GetErrorMessage()."</div>\n".
             "</div>";
    }

}
